package com.yusong.algorithm.array;

import java.util.Arrays;

public class MoveZeroes {


    public static void main(String[] args) {
        int[] grid = {0,0,1};

        MoveZeroes main = new MoveZeroes();

        main.moveZeroes(grid);

        System.out.println(Arrays.toString(grid));
    }

    /**
     * 快慢指针
     * @param nums
     */
    public void moveZeroesSlow(int[] nums) {
        if(nums == null || nums.length == 0){
            return;
        }
        int length = nums.length;
        int slow = 0;
        for(int i= 0; i < length; i++){
            int currentValue = nums[i];
            if(currentValue != 0){
                nums[slow++] = currentValue;
            }
        }
        for(int j = slow ; j < length; j++){
            nums[j] = 0;
        }

    }


    public void moveZeroes(int[] nums) {
        if(nums == null || nums.length == 0){
            return;
        }
        int length = nums.length;
        int length2 = length;
        int count = 0;
        for(int i =0; i< length2; i++){
            int currentValue = nums[i];
            if(currentValue == 0){
                count++;
                for(int j = i+1; j < length; j++){
                    nums[j-1]=nums[j];
                }
                length2--;
                i--;
            }

        }
        for(int i= 0; i < count; i++){
            nums[length-i-1] = 0;
        }
    }

    /**
     * 冒泡
     * @param nums
     */
    public void moveZeroes2(int[] nums) {
        if(nums == null || nums.length == 0){
            return;
        }
        int length = nums.length;
    }

    private void sortTo(int[] nums,int m, int n){
        if(m >= n){
            return;
        }
        for(int i=m; i<n; i++){
            switchNum(nums,i, i+1);
        }

    }

    private void switchNum(int[] nums,int m, int n){
        int temple = nums[m];
        nums[m] = nums[n];
        nums[n] = temple;
    }
}
